package com.solotiger.geographic.ui.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.solotiger.geographic.GeoApplication;
import com.solotiger.geographic.R;
import com.solotiger.geographic.util.ToastUtil;

import butterknife.ButterKnife;

/**
 * 所有activity的父类
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected Activity mContext;

    private Toolbar mToolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        setContentView(getLayoutResId());

        mContext = this;

        ButterKnife.bind(this);

        initToolBar();
    }

    private void initToolBar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }
    }

    protected Toolbar getToolbar() {
        return mToolbar;
    }

    protected void setTitle(String title) {
        if (mToolbar != null) {
            mToolbar.setTitle(title);
        }
    }

    protected void setNavigationOnClickListener(View.OnClickListener listener) {
        if (mToolbar != null) {
            mToolbar.setNavigationOnClickListener(listener);
        }
    }

    protected void showMessage(int rid) {
        ToastUtil.show(GeoApplication.getInstance(), rid);
    }

    protected void showMessage(String msg) {
        ToastUtil.show(GeoApplication.getInstance(), msg);
    }

    /**
     * 由子类执行后续操作
     *
     * @return
     */
    protected abstract int getLayoutResId();
}
